package com.zmm.myyimixian.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zmm.myyimixian.R;
import com.zmm.myyimixian.bean.FenLeiTitle;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wencai2015 on 2016/10/21.
 */

public class MyFLTitleApater extends BaseAdapter {
    private Context context;
    private List<FenLeiTitle.CategoriesBean> categories;
    private static int selectedId = 0;

    /**
     * 当用户选中某一项，或者按下up 或 down 按钮时调用
     * 将改特殊绘制的项的id传过来，然后notifyDataSetChanged()
     * 通知list重绘
     *
     * @param positon 选中项的id
     */
    public void changeSelected(int positon) {
        if (positon != selectedId) {
            selectedId = positon;
            notifyDataSetChanged();
        }
    }

    public MyFLTitleApater(Context context) {
        this.context = context;
    }

    public void setData(List<FenLeiTitle.CategoriesBean> categories) {
        this.categories = categories;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return categories.size();
    }

    @Override
    public Object getItem(int position) {
        return categories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.feilei_listview_itme, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (selectedId == position){
            convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
            viewHolder.titleSgTv.setTextColor(Color.parseColor("#ff0030"));
        }else {
            convertView.setBackgroundColor(Color.parseColor("#F0EFF4"));
            viewHolder.titleSgTv.setTextColor(Color.parseColor("#000000"));
        }
        FenLeiTitle.CategoriesBean categoriesBean = categories.get(position);
        viewHolder.titleSgTv.setText(categoriesBean.getName());
        viewHolder.ftitleSgTv.setText(categoriesBean.getSub_name());
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.title_sg_tv)
        TextView titleSgTv;
        @BindView(R.id.ftitle_sg_tv)
        TextView ftitleSgTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
